package cn.com.czpb.client.module.news.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;

import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.model.news.NewsChannelBean;
import cn.com.czpb.client.module.news.NewsBaseFragment;
import cn.com.czpb.client.module.news.ZixunFragment;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.StringUtils;

/**
 * Created by 猪猪侠 on 2018/5/3.
 */

public class NewsHtmlFragment extends NewsBaseFragment {
    @BindView(R.id.webview_id)
    WebView mywebview;
    private String mTitle;
    private NewsChannelBean channelbean;
    private int position;

    public static final NewsHtmlFragment newInstance(int position, NewsChannelBean newsChannelBean) {
        NewsHtmlFragment fragment = new NewsHtmlFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putSerializable("bean", newsChannelBean);
        fragment.setArguments(bundle);
        return fragment;
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initView() {
        channelbean = (NewsChannelBean) getArguments().getSerializable("bean");
        position = getArguments().getInt("position");

        WebSettings webSettings = mywebview.getSettings();
        String ua = webSettings.getUserAgentString();
        LogUtils.i("ua-->"+ua);
        webSettings.setUserAgentString(ua+"; gxrbapp/");
        String ua2 = webSettings.getUserAgentString();
        LogUtils.i("ua-->"+ua2);
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
//        mywebview.loadUrl("startAppSettings");

        mywebview.setWebViewClient(new WebViewClient(){
            @Override

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);

                //如果不需要其他对点击链接事件的处理返回true，否则返回false

                return true;

            }
        });
        mywebview.setWebChromeClient(new MyWebChromeClient());

        mywebview.loadUrl(channelbean.getLink());

        mywebview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int x, int y, int oldx, int oldy) {
                if (0==y){
                    ((ZixunFragment)NewsHtmlFragment.this.getParentFragment()).toVisbility();
                }
            }
        });
    }
    public class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
            final JsResult finalJsResult = jsResult;
            new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finalJsResult.confirm();
                }
            }).setCancelable(false).create().show();
            return true;
        }


    }

    @Override
    public void init() {
        if (!StringUtils.isEmpty(channelbean.getLink())){
            mywebview.loadUrl(channelbean.getLink());
        }
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public int setMyContentView() {
        return R.layout.news_html_fragment;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getTitle() {
        return mTitle;
    }
}
